package com.houhanzhi.controller;


import com.houhanzhi.controller.request.LinkGroupAddRequest;
import com.houhanzhi.controller.request.LinkGroupUpdateRequest;
import com.houhanzhi.enums.BizCodeEnum;
import com.houhanzhi.response.JsonData;
import com.houhanzhi.service.LinkGroupService;
import com.houhanzhi.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author houhanzhi
 * @since 2025-02-26
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;

    /**
     * 新增分组
     * @param request
     * @return
     */
    @PostMapping("/add")
    public JsonData add(@RequestBody LinkGroupAddRequest request) {
        int row = linkGroupService.add(request);
        return row > 0 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }

    /**
     * 根据id删除分组
     * @param groupId
     * @return
     */
    @DeleteMapping("/del/{group_id}")
    public JsonData del(@PathVariable("group_id") Long groupId) {

        int rows = linkGroupService.del(groupId);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    /**
     * 根据id查询详情
     * @param groupId
     * @return
     */
    @PostMapping("/detail/{group_id}")
    public JsonData detail(@PathVariable("group_id") Long groupId) {

        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }

    /**
     * 查询用户全部分组
     * @return
     */
    @GetMapping("/findUserAllLinkGroup")
    public JsonData findUserAllLinkGroup() {

        List<LinkGroupVO> list = linkGroupService.listAllLinkGroup();
        return JsonData.buildSuccess(list);
    }

    /**
     * 更新组名
     * @return
     */
    @PutMapping("/update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest linkGroupUpdateRequest) {

        int rows = linkGroupService.update(linkGroupUpdateRequest);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }



}

